package com.qk.controller;

import com.qk.anno.Log;
import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Dept;
import com.qk.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    // 查询部门列表（分页）
    @GetMapping
    public Result querylist(String name, Integer status,
                            @RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("查询部门名称：{},状态：{},页码：{},每页元素个数：{}", name, status, page, pageSize);
        // 调用service层
        PageResult<Dept> deptPageResult = deptService.selectlist(name, status, page, pageSize);
        return Result.success(deptPageResult);
    }

    // 删除部门
    @Log
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除部门：{}", id);
        deptService.delete(id);
        return Result.success();
    }

    // 添加部门
    @Log
    @PostMapping //限定请求方式
    public Result addDept(@RequestBody Dept dept) {
        log.info("添加部门：{}", dept);
        deptService.add(dept);
        return Result.success();
    }

    // 根据id查询部门
    @GetMapping("/{id}")
    public Result getByTd(@PathVariable Integer id) {
        log.info("查询部门：{}", id);
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    // 修改部门
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        log.info("修改部门：{}", dept);
        deptService.update(dept);
        return Result.success();
    }

    // 查询所有部门（不分页）
    @GetMapping("/list")
    public Result queryAllList(Dept dept) {
        log.info("查询所有部门：" );
        List<Dept> list = deptService.list();
        return Result.success(list);
    }
}
